home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / AEObject-Edition1.0.2 Sample / Initialize.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-09  |  13.3 KB  |  347 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. *
  3. *  Apple Developer Technical Support
  4. *
  5. *  
  6. *
  7. *  Program:    AEObject-Edition Sample
  8. *  File:   Initialize.c -C Source
  9. *
  10. *  by: C.K. Haun <TR>
  11. *
  12. *  Copyright © 1990-1992 Apple Computer, Inc.
  13. *  All rights reserved.
  14. *
  15. *------------------------------------------------------------------------------
  16. * This file contains some of our window handling routines.  I created it 
  17. * primarily to take some of the clutter out of the main.c file.
  18. * It also contains the clipboard handlers
  19. *----------------------------------------------------------------------------*/
  20.  
  21. #define __INIT__
  22.  
  23. #include "Sampdefines.h"
  24.  
  25. #pragma segment MyInit
  26. /* StartStuff initializes the managers and does some other init stuff */
  27. void StartStuff(void)
  28. {
  29.     long aLong;
  30.     OSErr myErr;    
  31.     MaxApplZone();
  32.     MaxMem(&aLong);
  33.     /* Nobody every says this, it's always assumed, but I'll tell yah... */
  34.     /* 'qd' is in the Runtime.o library (which is one reason you have to link it)  */
  35.     /* it doesn't come from the toolbox or any other magic place */
  36.     InitGraf((Ptr)&qd.thePort);
  37.     InitFonts();
  38.     InitWindows();
  39.     InitMenus();
  40.     TEInit();
  41.     InitDialogs(nil);
  42.     MoreMasters();
  43.     /* See if AppleEvents and the Edition manager are available */
  44.     InitAEStuff();                                          /* In AppleEventM.c */
  45.     gHasEditionManager = (Gestalt(gestaltEditionMgrAttr, &aLong) == noErr);
  46.     /* the value returned will be 1, but we don't really care here, since there aren't */
  47.     /* multiple versions of the Edition Manager yet */
  48.     if (gHasEditionManager) {
  49.         InitEditionPack();
  50.         
  51.     } else {
  52.         Alert(kNoEditionManager,(ModalFilterProcPtr) standardAlertFilter);                      /* tell 'em and exit */
  53.         ExitToShell();
  54.     }
  55.     GetCurrentProcess(&gOurSN);                             /* grab our applications process serial number */
  56.     /* so we can communicate (send appleevents) to */
  57.     /* ourself easily */
  58.     myErr = Gestalt(gestaltQuickdrawVersion, &aLong);
  59.     if(myErr == noErr && (aLong & 0xff00)) 
  60.         gHasColor = true;
  61.     StartMenus();                                           /* load the menu bar and set up the menus */
  62.  
  63.     CreateClipBoard();
  64.  
  65.     CreateAEStatus();
  66.     /* ••• NOTE: I am not opening a document window here.  The application will wait for an */
  67.     /* 'oapp' or 'odoc' AppleEvent before opening a window.  This makes it a whole lot easier */
  68.     /* to deal with ODOC events, since you needn't special case anything */
  69.     gMySleep = 5;                                           /* initialize sleep at some reasonable level */
  70.     gWindObjSpecHandle = (WindowObjectDefHandle)NewHandleClear(sizeof(WindowObjectDef));
  71.     gTextObjSpecHandle = (TextObjectDefHandle)NewHandleClear(sizeof(TextObjectDef));
  72.     gShapeObjSpecHandle = (ShapeObjectDefHandle)NewHandleClear(sizeof(ShapeObjectDef));
  73.     gLocalInteraction = 0;    /* default this */
  74.     gAESendInteraction = 0;
  75.     gReplyMode = 1;
  76.     LoadPrefs();
  77.     gPreferences.prefsChanged = false;
  78.     /* and set it, it may have changed with the Prefs */
  79.      AESetInteractionAllowed(gLocalInteraction);
  80.     InitCursor();
  81.  
  82. }
  83.  
  84. /* end StartStuff */
  85.  
  86.  
  87. #pragma segment MyInit
  88.  
  89.  
  90. /* CreateClipBoard creates our invisible clipboard window at application */
  91. /* start time, and keeps it open always.  Visibility is toggled from the edit menu, */
  92. /* but the window always exists */
  93.  
  94. void CreateClipBoard(void)
  95. {
  96.     WindowPtr tempWindow;
  97.     windowCHandle tempWC;
  98.     
  99.     /* open my clipboard window */
  100.     tempWindow = GetNewWindow(kClipWindow, 0, (WindowPtr)-1);
  101.     
  102.     /* since I'm using the same function for my clipboard as for a document window, I have */
  103.     /* to change some of the fields */
  104.     ((WindowPeek)tempWindow)->windowKind = kClipboardWindow;
  105.     ((WindowPeek)tempWindow)->refCon = NewHandleClear(sizeof(windowControl));        /* add our control structure to it */
  106.     tempWC = (windowCHandle)GetWRefCon(tempWindow);         /* and put it where we can use it */
  107.     HLock((Handle)tempWC);                                  /* lock it down */
  108.     /* add pointers to our procedures for drawing, saving, and closing */
  109.     (*tempWC)->drawMe = (ProcPtr)DrawClip;
  110.     (*tempWC)->saveMe = (ProcPtr)nil;
  111.     (*tempWC)->closeMe = (ProcPtr)CloseClip;
  112.     (*tempWC)->clickMe = (ProcPtr)ClipClick;
  113.     (*tempWC)->sizeMe = (ProcPtr)SizeClip;
  114.     (*tempWC)->currentAction = nil;
  115.     (*tempWC)->undoAction = nil;
  116.     (*tempWC)->hasSelection = false;
  117.     (*tempWC)->windowDirty = false;
  118.     (*tempWC)->boxHandle = nil;
  119.     (*tempWC)->textSections = nil;
  120.     HUnlock((Handle)tempWC);
  121.     gScrapData = NewHandle(0);
  122.     UpdateScrap(false);                                     /* not visible, don't refresh */
  123. }
  124.  
  125. /* end CreateClipBoard */
  126.  
  127.  
  128. /* CreateAEStatus creates our invisible AEStatus window at application */
  129. /* start time, and keeps it open always.  Visibility is toggled from the edit menu, */
  130. /* but the window always exists */
  131. void CreateAEStatus(void)
  132. {
  133.     WindowPtr tempWindow;
  134.     windowCHandle tempWC;
  135.     WindowPtr tempPort;
  136.     Rect dest;
  137.     /* open my clipboard window */
  138.     tempWindow = GetNewWindow(kAEStatusWindowID, 0, (WindowPtr)-1);
  139.     ((WindowPeek)tempWindow)->windowKind = kAEStatusWindow;
  140.     /* since I'm using the same function for my clipboard as for a document window, I have */
  141.     /* to change some of the fields */
  142.     ((WindowPeek)tempWindow)->refCon = NewHandleClear(sizeof(windowControl));        /* add our control structure to it */
  143.     tempWC = (windowCHandle)GetWRefCon(tempWindow);         /* and put it where we can use it */
  144.     HLock((Handle)tempWC);                                  /* lock it down */
  145.     /* add pointers to our procedures for drawing, saving, and closing */
  146.     (*tempWC)->drawMe = (ProcPtr)DrawAES;
  147.     (*tempWC)->saveMe = (ProcPtr)SaveAES;
  148.     (*tempWC)->closeMe = (ProcPtr)CloseAES;
  149.     (*tempWC)->clickMe = (ProcPtr)ClickAES;
  150.     (*tempWC)->sizeMe = (ProcPtr)SizeAES;
  151.     (*tempWC)->currentAction = nil;
  152.     (*tempWC)->undoAction = nil;
  153.     (*tempWC)->hasSelection = false;
  154.     (*tempWC)->windowDirty = false;
  155.     
  156.     (*tempWC)->textSections = nil;
  157.     (*tempWC)->fileAliasHandle = (AliasHandle)NewHandle(0);
  158.     (*tempWC)->windowDirty = false;
  159.     (*tempWC)->windowIndex = 999999; /* make abnormally large */
  160.     HUnlock((Handle)tempWC);
  161.     /* and this window has a  text edit field */
  162.     GetPort(&tempPort);                                     /* save and set port so TextEdit puts the record in the right place */
  163.     SetPort(tempWindow);
  164.     
  165.     dest = tempWindow->portRect;
  166.     dest.right -= 16;
  167.     (*tempWC)->boxHandle = TENew(&dest, &dest);
  168.     TEAutoView(true, (*tempWC)->boxHandle);                 /* make the text auto-scrolling */
  169.     SetPort(tempPort);
  170. }
  171.  
  172. /* StartMenus loads our menu bar resource, and initializes the menus 
  173. *   and some settings related to the menu items 
  174. */
  175.  
  176. void StartMenus(void)
  177. {
  178.     MenuHandle tempHandle;
  179.     StringHandle helpString;
  180.     Handle myMBar;
  181.     short count;
  182.     myMBar = GetNewMBar(kOurMenuBar);
  183.     SetMenuBar(myMBar);
  184.     gAppleMenuHandle = GetMHandle(kAppleMenu);
  185.     gFileMenuHandle = GetMHandle(kFileMenu);
  186.     gEditMenuHandle = GetMHandle(kEditMenu);
  187.     gToolMenuHandle = GetMHandle(kToolsMenu);
  188.     gAppleEventMenuHandle = GetMHandle(kAEMenu);
  189.     gEditionMenuHandle = GetMHandle(kEditionMenu);
  190.     gWindowMenuHandle = GetMHandle(kWindowMenu);
  191.     if(gHasColor){
  192.         gColorMenuHandle = GetMenu(kColorMenu);
  193.         InsertMenu(gColorMenuHandle,0);
  194.         CheckItem(gColorMenuHandle,kBlackColorItem,true);
  195.         }
  196.         DrawMenuBar();    
  197.     /* The following lines add my Help item to the Sys 7 Help menu. */
  198.     /* That's where help should go now, _not_ under the Apple Menu as we have  */
  199.     /* been doing for so long.  Since we now have an actual Help menu, use it, */
  200.     /* since your users will expect it. */
  201.     /* get the Help menu handle */
  202.     HMGetHelpMenuHandle(&tempHandle);
  203.     /* How many items does it have? */
  204.     count = CountMItems(tempHandle);
  205.     /* get our help string from our resource fork */
  206.     helpString = GetString(kHelpString);
  207.     DetachResource(helpString);
  208.     HNoPurge(helpString);
  209.     MoveHHi((Handle)helpString);
  210.     HLock((Handle)helpString);
  211.     /* Add our help item to the help menu */
  212.     InsMenuItem(tempHandle, (Ptr)*helpString, count + 1);
  213.     HUnlock(helpString);
  214.     HPurge(helpString);
  215.     /* Remember what our help item number is */
  216.     gHelpItem = CountMItems(tempHandle);
  217.     
  218.     /* add heirarchical menus for AppleEvent menu */
  219.     tempHandle = GetMenu(kGetDataSubmenu);
  220.     InsertMenu(tempHandle, -1);
  221.     /* and the setdata hier */
  222.     tempHandle = GetMenu(kSetDataSubmenu);
  223.     InsertMenu(tempHandle, -1);
  224.     
  225.     AddResMenu(gAppleMenuHandle, 'DRVR');                   /* add DAs */
  226. }
  227.  
  228. /* end StartMenus */
  229.  
  230. /* InitAEStuff checks to see if this machine has AppleEvents and 
  231. *   does our setup.
  232. *   If AppleEvents are not found, we alert and exit.
  233. *   This is also the place where all the handlers for AppleEvents we deal
  234. *   with are installed.  This includes the required AppleEvents, and the
  235. *   Edition Manager specific ones used in this app.
  236. */
  237. #pragma segment MyInit
  238. void InitAEStuff(void)
  239. {
  240.     extern Boolean gHasAppleEvents;
  241.     long aLong;
  242.     AEinstalls HandlersToInstall[] =  {
  243.         /* The above are the four required AppleEvents. */ {
  244.             kCoreEventClass, kAEOpenApplication, AEOpenHandler
  245.         },  {
  246.             kCoreEventClass, kAEOpenDocuments, AEOpenDocHandler
  247.         },  {
  248.             kCoreEventClass, kAEPrintDocuments, AEPrintHandler
  249.         },  {
  250.             kCoreEventClass, kAEQuitApplication, AEQuitHandler
  251.         }, 
  252.         /* These are the five Edition Manager AppleEvents  */ {
  253.             kCoreEventClass, kAECreatePublisher, AECreatePubHandler
  254.         },  {
  255.             sectionEventMsgClass, sectionReadMsgID, AEReadSectionHandler
  256.         },  {
  257.             sectionEventMsgClass, sectionWriteMsgID, AEWriteSectionHandler
  258.         },  {
  259.             sectionEventMsgClass, sectionScrollMsgID, AEScrollSectionHandler
  260.         },  {
  261.             sectionEventMsgClass, sectionCancelMsgID, AECancelSectionHandler
  262.         }, 
  263.         /*  some core event handlers */ {
  264.             kAECoreSuite, kAEGetData, AEGetDataHandler
  265.         },  {
  266.             kAECoreSuite, kAESetData, AESetDataHandler
  267.         },  {
  268.             kAECoreSuite, kAEAnswer, AEAnswerHandler
  269.         }
  270.     };
  271.     
  272.     static CoercionInstalls CoercionsToInstall[] =  {
  273.         {
  274.             typeAlias, typeTargetID, CoerceAliasToTargetID, false
  275.         },  {
  276.             typeBoolean, typeChar, CoerceBooleanToChar, false
  277.         },  {
  278.             typeQDRectangle, typeChar, CoerceQDRectToChar, false
  279.         }, 
  280.         /* and two little coercions I just had to have... */ {
  281.             typeChar, typeMyPString, CoerceCharToPString, false
  282.         },  {
  283.             typeMyPString, typeChar, CoercePStringToChar, false
  284.         }
  285.     };
  286.     OSErr aevtErr = noErr;
  287.     aLong = 0;
  288.     /* Check this machine for AppleEvents.  If they are not here (ie not 7.0)
  289.     *   then we exit */
  290.     gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &aLong) == noErr);
  291.     /* The following series of calls installs all our AppleEvent Handlers.
  292.     *   These handlers are added to the application event handler list that 
  293.     *   the AppleEvent manager maintains.  So, whenever an AppleEvent happens
  294.     *   and we call AEProcessEvent, the AppleEvent manager will check our
  295.     *   list of handlers and dispatch to it if there is one.
  296.     */
  297.     if (gHasAppleEvents) {
  298.         register qq;
  299.         for (qq = 0; qq < ((sizeof(HandlersToInstall) / sizeof(AEinstalls))); qq++) {
  300.             aevtErr = AEInstallEventHandler(HandlersToInstall[qq].theClass, HandlersToInstall[qq].theEvent,
  301.                                             HandlersToInstall[qq].theProc, 0, false);
  302.             
  303.             if (aevtErr) {
  304.                 Str31 text1, text2;
  305.                 NumToString((long)aevtErr, text1);
  306.                 text2[0] = 4;
  307.                 BlockMove((Ptr)&HandlersToInstall[qq].theEvent, (Ptr)text2, 4);
  308.                 ParamText(text1, text2, "", "");
  309.                 NoteAlert(kBadHInstall, (ModalFilterProcPtr)standardAlertFilter);
  310.             }
  311.         }
  312.         /* now our coercion routines */
  313.         for (qq = 0; qq < ((sizeof(CoercionsToInstall) / sizeof(AEinstalls))); qq++) {
  314.             aevtErr = AEInstallCoercionHandler(CoercionsToInstall[qq].fromType, CoercionsToInstall[qq].toType,
  315.                                                CoercionsToInstall[qq].theProc, nil, false, false);
  316.             if (aevtErr) {
  317.                 Str31 text1, text2;
  318.                 NumToString((long)aevtErr, text1);
  319.                 text2[0] = 4;
  320.                 BlockMove((Ptr)&CoercionsToInstall[qq].fromType, (Ptr)text2, 4);
  321.                 ParamText(text1, text2, "", "");
  322.                 NoteAlert(kBadHInstall, (ModalFilterProcPtr)standardAlertFilter);
  323.             }
  324.         }
  325.        
  326.            } else {
  327.         Alert(kNoAppleEvents, (ModalFilterProcPtr)standardAlertFilter);
  328.         ExitToShell();
  329.     }
  330.     /* this function initializes the OSL and installs our handlers */
  331.     aevtErr = InstallObjectHandlers();
  332.     if (aevtErr) {
  333.         
  334.         ShowMe("\p Object Lib error, failing and exiting", aevtErr, __LINE__);
  335.         ExitToShell();
  336.     }
  337.     /* and one more thing, create my null descriptor to build all my objects from. */
  338.     /* you may want to do this each time, personal choice */
  339.     aevtErr = AECreateDesc(typeNull, nil, 0, &gNullDesc);
  340.     mAEErrorDisplay("\pCreating nul desc", aevtErr)
  341. }
  342.  
  343. /* end InitAEStuff */
  344.  
  345.  
  346. #undef __INIT__
  347.